﻿using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using Runda.EShop.Common.Utility;
using Runda.EShop.Common.Entity;

namespace Runda.EShop.Dal
{
    /// <summary>
    /// DAL layer class dbo.Bas_Product_Cate.
    /// </summary>
    public partial class BasProductCateDal 
    {
        public void ProductCateSync()
        {
            DbCommand dbcommand = CurrentDatabase.GetSqlStringCommand("usp_ProductCateSync");
            CurrentDatabase.ExecuteNonQuery(dbcommand);
        }

        public DataTable BuildProductCategory(Guid CustomerID)
        {
            string sql = @"select A.*, 
case when B.CustomerID IS NULL then 0 else 1 end as IsChecked
from Bas_Product_Cate A
left join Relation_Customer_ProductCate B ON A.CateCode = B.CateCode and B.CustomerID =@CustomerID";
            DbCommand dbcommand = CurrentDatabase.GetSqlStringCommand(sql);
            CurrentDatabase.AddInParameter(dbcommand, "@CustomerID", DbType.Guid, CustomerID);
           return CurrentDatabase.ExecuteDataSet(dbcommand).Tables[0];
        }

        public DataTable BuildProductCategory(Guid CustomerID,int CateType)
        {
            string sql = @"select A.*, 
case when B.CustomerID IS NULL then 0 else 1 end as IsChecked
from Bas_Product_Cate A
left join Relation_Customer_ProductCate B ON A.CateCode = B.CateCode and B.CustomerID =@CustomerID and b.cateType = @cateType where a.cateType = @cateType ";
            DbCommand dbcommand = CurrentDatabase.GetSqlStringCommand(sql);
            CurrentDatabase.AddInParameter(dbcommand, "@CustomerID", DbType.Guid, CustomerID);
            CurrentDatabase.AddInParameter(dbcommand, "@cateType", DbType.Int16, CateType);
            return CurrentDatabase.ExecuteDataSet(dbcommand).Tables[0];
        }

        public List<BasProductCateEntity> GetProductCateListByCustomerID(Guid CustomerID)
        {
            List< BasProductCateEntity> list = null;
            string sql = @"select B.* from Relation_Customer_ProductCate A
JOIN Bas_Product_Cate B ON A.CateCode = B.CateCode WHERE A.CustomerID = @CustomerID";

            DbCommand dbcommand = CurrentDatabase.GetSqlStringCommand(sql);
            CurrentDatabase.AddInParameter(dbcommand, "@CustomerID", DbType.Guid, CustomerID);

            using (IDataReader reader = CurrentDatabase.ExecuteReader(dbcommand))
            {
                list = this.GetListFromReader<BasProductCateEntity>(reader);
            }

            return list;
        }

        public List<BasProductCateEntity> GetProductCateListByCustomerID(Guid CustomerID,int CateType)
        {
            List<BasProductCateEntity> list = null;
            string sql = @"select B.* from Relation_Customer_ProductCate A
JOIN Bas_Product_Cate B ON A.CateCode = B.CateCode WHERE (B.Deleted is null or B.Deleted=0) and A.CustomerID = @CustomerID and b.CateType=@CateType and a.CateType=@CateType";

            DbCommand dbcommand = CurrentDatabase.GetSqlStringCommand(sql);
            CurrentDatabase.AddInParameter(dbcommand, "@CustomerID", DbType.Guid, CustomerID);
            CurrentDatabase.AddInParameter(dbcommand, "@CateType", DbType.Int16, CateType);

            using (IDataReader reader = CurrentDatabase.ExecuteReader(dbcommand))
            {
                list = this.GetListFromReader<BasProductCateEntity>(reader);
            }

            return list;
        }
    }
}
